// 对ema排序
function createEmaList (info) {
  let list = [
    { name: '5', value: info.ema5 },
    { name: '10', value: info.ema10 },
    { name: '20', value: info.ema20 }
  ]
  list.sort((a, b) => {
    return a.value - b.value
  })
  let text = list.map((v) => {
    return v.name
  }).join('<')
  return {
    list,
    text
  }
}

function getSgRate (em) {
  if (em.sg >= 1) {
    return 1
  } else if (em.sg >= 0.5) {
    return 0.66
  }
  return 0.33
}

// 均线分数
function countAverageScore (em) {
  let fen = 0
  if (em.kdjDiff > 0) {
    fen += 0.2
  } else {
    fen -= 0.2
  }
  if (em.macd > 0) {
    fen += 0.4
  } else {
    fen -= 0.4
  }
  let ema = getSgRate(em) * 0.4
  if (em.ema5 > em.ema10 && em.ema10 > em.ema20) {
    fen += ema
  } else if (em.ema5 < em.ema10 && em.ema10 < em.ema20) {
    fen -= ema
  }
  return fen
}

function getTendency (em) {
  if (em.macd > 0 && em.ema5 > em.ema10 && em.ema10 > em.ema20) {
    if (em.isKdjIncrease || em.isMacdIncrease) {
      return 'duo'
    }
  }
  if (em.macd < 0 && em.ema5 < em.ema10 && em.ema10 < em.ema20) {
    if (em.isMacdDecline || em.isKdjDecline) {
      return 'kong'
    }
  }
  return ''
}

function getTendencyScore (em) {
  if (em.macd > 0 && em.ema5 > em.ema10 && em.ema10 > em.ema20) {
    if (em.isKdjIncrease && em.isMacdIncrease) {
      return 1
    }
    if (em.isKdjIncrease || em.isMacdIncrease) {
      return 0.8
    }
  }
  if (em.macd < 0 && em.ema5 < em.ema10 && em.ema10 < em.ema20) {
    if (em.isMacdDecline && em.isKdjDecline) {
      return -1
    }
    if (em.isMacdDecline || em.isKdjDecline) {
      return -0.8
    }
  }
  if (em.isKdjIncrease && em.isMacdIncrease) {
    if (em.macd > 0 || em.ema5 > em.ema10 || em.ema5 > em.ema20) {
      return 0.6
    }
  }
  if (em.isMacdDecline && em.isKdjDecline) {
    if (em.macd < 0 || em.ema5 < em.ema10 || em.ema5 < em.ema20) {
      return -0.6
    }
  }
  if (em.isKdjIncrease || em.isMacdIncrease) {
    if ((em.macd > 0 && em.ema5 > em.ema10) || (em.ema5 > em.ema10 && em.ema10 > em.ema20)) {
      return 0.4
    }
    if (em.macd > 0 || em.ema5 > em.ema10 || em.ema5 > em.ema20) {
      return 0.2
    }
  }
  if (em.isMacdDecline || em.isKdjDecline) {
    if ((em.macd < 0 && em.ema5 < em.ema10) || (em.ema5 < em.ema10 && em.ema10 < em.ema20)) {
      return -0.4
    }
    if (em.macd < 0 || em.ema5 < em.ema10 || em.ema5 < em.ema20) {
      return -0.2
    }
  }
  return 0
}
// function hasEmaInfo (emaInfo) {
//   return emaInfo.kdjDiff && emaInfo.macd && (emaInfo.isMacdIncrease || emaInfo.isMacdDecline)
// }
// 分析个体
const countItemInfo = function (list) {
  list.forEach((v) => {
    // 格式化指标
    v.monthEmaInfo.emaList = createEmaList(v.monthEmaInfo).list
    v.monthEmaInfo.emaText = createEmaList(v.monthEmaInfo).text
    v.monthEmaInfo.kdjDiff = 0
    if (v.monthEmaInfo.kdj) {
      v.monthEmaInfo.kdjDiff = v.monthEmaInfo.kdj.J - v.monthEmaInfo.kdj.D
    }
    v.weekEmaInfo.emaList = createEmaList(v.weekEmaInfo).list
    v.weekEmaInfo.emaText = createEmaList(v.weekEmaInfo).text
    v.weekEmaInfo.kdjDiff = 0
    if (v.weekEmaInfo.kdj) {
      v.weekEmaInfo.kdjDiff = v.weekEmaInfo.kdj.J - v.weekEmaInfo.kdj.D
    }
    v.dayEmaInfo.emaList = createEmaList(v.dayEmaInfo).list
    v.dayEmaInfo.emaText = createEmaList(v.dayEmaInfo).text
    v.dayEmaInfo.kdjDiff = 0
    if (v.dayEmaInfo.kdj) {
      v.dayEmaInfo.kdjDiff = v.dayEmaInfo.kdj.J - v.dayEmaInfo.kdj.D
    }
    v.hour4EmaInfo.emaList = createEmaList(v.hour4EmaInfo).list
    v.hour4EmaInfo.emaText = createEmaList(v.hour4EmaInfo).text
    v.hour4EmaInfo.kdjDiff = 0
    if (v.hour4EmaInfo.kdj) {
      v.hour4EmaInfo.kdjDiff = v.hour4EmaInfo.kdj.J - v.hour4EmaInfo.kdj.D
    }
    v.hourEmaInfo.emaList = createEmaList(v.hourEmaInfo).list
    v.hourEmaInfo.emaText = createEmaList(v.hourEmaInfo).text
    v.hourEmaInfo.kdjDiff = 0
    if (v.hourEmaInfo.kdj) {
      v.hourEmaInfo.kdjDiff = v.hourEmaInfo.kdj.J - v.hourEmaInfo.kdj.D
    }
    // v.minute15EmaInfo.emaList = createEmaList(v.minute15EmaInfo).list
    // v.minute15EmaInfo.emaText = createEmaList(v.minute15EmaInfo).text
    // v.minute15EmaInfo.kdjDiff = 0
    // if (v.minute15EmaInfo.kdj) {
    //   v.minute15EmaInfo.kdjDiff = v.minute15EmaInfo.kdj.J - v.minute15EmaInfo.kdj.D
    // }
    // 打分
    v.monthAverageScore = countAverageScore(v.monthEmaInfo)
    v.weekAverageScore = countAverageScore(v.weekEmaInfo)
    v.dayAverageScore = countAverageScore(v.dayEmaInfo)
    v.hour4AverageScore = countAverageScore(v.hour4EmaInfo)
    v.hourAverageScore = countAverageScore(v.hourEmaInfo)
    // v.minute15AverageScore = countAverageScore(v.minute15EmaInfo)
    v.tendency = getTendency(v.weekEmaInfo)
    v.tendencyScore = (getTendencyScore(v.weekEmaInfo) * (9 / 13)) +
      (getTendencyScore(v.dayEmaInfo) * (3 / 13)) +
      (getTendencyScore(v.hour4EmaInfo) * (1 / 13))
    // 拼接
    v.emList = [
      // {
      //   title: '15分钟',
      //   emaInfo: v.minute15EmaInfo,
      //   averageScore: parseInt(v.minute15AverageScore * 100)
      // },
      {
        title: '小时',
        emaInfo: v.hourEmaInfo,
        averageScore: parseInt(v.hourAverageScore * 100)
      },
      {
        title: '4小时',
        emaInfo: v.hour4EmaInfo,
        averageScore: parseInt(v.hour4AverageScore * 100)
      },
      {
        title: '天',
        emaInfo: v.dayEmaInfo,
        averageScore: parseInt(v.dayAverageScore * 100)
      },
      {
        title: '周',
        emaInfo: v.weekEmaInfo,
        averageScore: parseInt(v.weekAverageScore * 100)
      },
      {
        title: '月',
        emaInfo: v.monthEmaInfo,
        averageScore: parseInt(v.monthAverageScore * 100)
      }
    ]
  })
}

const createTotalInfoModel = function (tar) {
  let raw = {
    // 禁用
    disabledCount: 0,
    enabledCount: 0,
    // 均线分
    monthAverageScore: 0,
    weekAverageScore: 0,
    dayAverageScore: 0,
    hour4AverageScore: 0,
    hourAverageScore: 0,
    minute15AverageScore: 0
  }
  if (tar) {
    raw = Object.assign(raw, tar)
  }
  return raw
}
// 统计整体信息
const countTotalInfo = function (list) {
  const totalInfo = createTotalInfoModel()
  let monthAverageScore = 0
  let weekAverageScore = 0
  let dayAverageScore = 0
  let hour4AverageScore = 0
  let hourAverageScore = 0
  let minute15AverageScore = 0

  let monthAverageScoreWithSg = 0
  let weekAverageScoreWithSg = 0
  let dayAverageScoreWithSg = 0
  let hour4AverageScoreWithSg = 0
  let hourAverageScoreWithSg = 0
  let minute15AverageScoreWithSg = 0
  let positionFen = 0
  list.forEach((v) => {
    if (v.status === 1) {
      positionFen += v.tendencyScore || 0
    }
    // 禁用
    if (v.status === 1 && v.hy_status !== 1) {
      totalInfo.disabledCount++
    }
    if (v.status === 1 && v.hy_status === 1) {
      totalInfo.enabledCount++
      monthAverageScore += v.monthAverageScore
      weekAverageScore += v.weekAverageScore
      dayAverageScore += v.dayAverageScore
      hour4AverageScore += v.hour4AverageScore
      hourAverageScore += v.hourAverageScore
      // minute15AverageScore += v.minute15AverageScore

      monthAverageScoreWithSg += v.monthAverageScore * getSgRate(v)
      weekAverageScoreWithSg += v.weekAverageScore * getSgRate(v)
      dayAverageScoreWithSg += v.dayAverageScore * getSgRate(v)
      hour4AverageScoreWithSg += v.hour4AverageScore * getSgRate(v)
      hourAverageScoreWithSg += v.hourAverageScore * getSgRate(v)
      // minute15AverageScoreWithSg += v.minute15AverageScore * getSgRate(v)
    }
  })
  const enabledCount = totalInfo.enabledCount
  totalInfo.monthAverageScore = (monthAverageScore * 100) / enabledCount
  totalInfo.weekAverageScore = (weekAverageScore * 100) / enabledCount
  totalInfo.dayAverageScore = (dayAverageScore * 100) / enabledCount
  totalInfo.hour4AverageScore = (hour4AverageScore * 100) / enabledCount
  totalInfo.hourAverageScore = (hourAverageScore * 100) / enabledCount
  totalInfo.minute15AverageScore = (minute15AverageScore * 100) / enabledCount

  totalInfo.monthAverageScoreWithSg = (monthAverageScoreWithSg * 100) / enabledCount
  totalInfo.weekAverageScoreWithSg = (weekAverageScoreWithSg * 100) / enabledCount
  totalInfo.dayAverageScoreWithSg = (dayAverageScoreWithSg * 100) / enabledCount
  totalInfo.hour4AverageScoreWithSg = (hour4AverageScoreWithSg * 100) / enabledCount
  totalInfo.hourAverageScoreWithSg = (hourAverageScoreWithSg * 100) / enabledCount
  totalInfo.minute15AverageScoreWithSg = (minute15AverageScoreWithSg * 100) / enabledCount
  totalInfo.positionFen = parseInt((positionFen / list.length) * 100)
  return totalInfo
}

module.exports = {
  countItemInfo,
  countTotalInfo,
  createTotalInfoModel
}
